package com.ego.ware.controller;

import java.util.Arrays;

import com.ego.common.constant.SystemConstant;
import com.ego.common.vo.PageVo;
import com.ego.ware.vo.PurchaseDetailSearchVo;
import com.ego.ware.vo.PurchaseDetailVo;
import com.ego.ware.vo.PurchaseMergeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ego.ware.po.PurchaseDetail;
import com.ego.ware.service.PurchaseDetailService;
import com.ego.common.util.Result;

import javax.validation.Valid;

/**
 * @author KamTang
 * @program: ego
 * @description 采购需求控制器
 * @date 2022-11-13 13:30:29
 */
@RestController
@RequestMapping("ware/purchase-detail")
public class PurchaseDetailController {

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    /**
     * 搜索
     */
    @PostMapping("/search")
    public Result search(@Valid @RequestBody PurchaseDetailSearchVo search) {
        PageVo<PurchaseDetailVo> page = purchaseDetailService.search(search);
        return Result.ok().put("page", page);
    }

    /**
     * 合并到采购单
     */
    @PostMapping("/merge")
    public Result merge(@Valid @RequestBody PurchaseMergeVo merge) {
        boolean flag = purchaseDetailService.merge(merge);
        if (flag) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    /**
     * 根据id获取具体采购需求信息
     */
    @GetMapping("/get/{id}")
    public Result get(@PathVariable("id") Long id) {
        PurchaseDetailVo purchaseDetailVo = purchaseDetailService.get(id);
        return Result.ok().put("detail", purchaseDetailVo);
    }

    /**
     * 新增采购需求
     */
    @PostMapping("/save")
    public Result save(@RequestBody PurchaseDetailVo purchaseDetailVo) {
        try {
            purchaseDetailService.save(purchaseDetailVo);
        } catch (Exception e) {
            return Result.error(SystemConstant.Status.WARE_EXCEPTION.getCode(), SystemConstant.Status.WARE_EXCEPTION.getMsg());
        }
        return Result.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public Result update(@RequestBody PurchaseDetail purchaseDetail) {
        purchaseDetailService.updateById(purchaseDetail);
        return Result.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] ids) {
        purchaseDetailService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
